package com.zzyq.pc.web.configure.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzyq.pc.common.plugin.db.PageView;
import com.zzyq.pc.web.configure.mapper.CompanyMapper;
import com.zzyq.pc.web.configure.mapper.DepartMapper;
import com.zzyq.pc.web.configure.model.Company;
import com.zzyq.pc.web.configure.service.ICompanyService;
import com.zzyq.pc.web.user.mapper.UserMapper;

@Service
public class CompanyServiceImpl implements ICompanyService {
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    DepartMapper departMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<Company> selectAsList(PageView pageView, Company company) {
        Map<String, Object> map = new HashMap<>();
        if (pageView != null) {
            map.put("paging", pageView);
        }
        map.put("companyName", company.getCompanyName());
        map.put("companyBrie", company.getCompanyBrie());
        return companyMapper.selectAsList(map);
    }

    @Override
    public Company selectById(Integer companyId) {
        if (companyId == null) {
            return null;
        }
        return companyMapper.selectById(companyId);
    }

    @Override
    public List<Company> selectSameCompany(Company company) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", company.getCompanyId());
        map.put("companyName", company.getCompanyName());
        return companyMapper.selectSameCompany(map);
    }

    @Override
    public boolean addCompany(Company company) {
        if (StringUtils.isBlank(company.getCompanyBrie())) {
            company.setCompanyBrie(company.getCompanyName());
        }
        company.setIsDelete(0);
        return companyMapper.addCompany(company) > 0;
    }

    @Override
    public boolean updateCompany(Company company) {
        return companyMapper.updateCompany(company) > 0;
    }

    @Override
    public boolean deleteCompany(Integer companyId) {
        boolean result = companyMapper.deleteCompany(companyId) > 0;
        // 删除部门和用户信息
        if (result) {
            departMapper.deleteByComId(companyId);
            userMapper.deleteByComId(companyId);
        }
        return result;
    }

    @Override
    public List<Integer> selectAllCompanyId() {
        return companyMapper.selectAllCompanyId();
    }
}
